﻿using System;
using System.Threading;
using Columbus;

namespace HelloColumbusApplication
{
    public class HomeController : Controller
    {
        public ActionResult HomePage(HomePageMessage message)
        {
            // Specifying view name is not required if view name is the same as controller action name
            return Page(new HomePageViewModel(message.Text));
        }


        public ActionResult SayHelloTo(string name)
        {
            return Page("GreetingPage", String.Format("Hello {0}!", name));
        }

        [Cancelable]
        public ActionResult AboutDialog()
        {
            // Disallowing for now to allow later in the action
            DisallowCancelling();

            ReportProgress("Let's pretend it is a long running action");
            Thread.Sleep(3000);
            ReportProgress("This is the action execution progress");
            Thread.Sleep(3000);
            ReportProgress("Progress is just an object");
            Thread.Sleep(3000);
            ReportProgress("So you can put something like '65% left' here");
            Thread.Sleep(3000);
            ReportProgress("Or images");
            Thread.Sleep(3000);
            ReportProgress("Or action intermediate results");
            Thread.Sleep(3000);
            ReportProgress("Just call ReportProgress from your action");
            Thread.Sleep(3000);
            ReportProgress("You can even make the action cancelable");
            Thread.Sleep(3000);
            ReportProgress("Or just certain parts of the action");
            Thread.Sleep(3000);

            AllowCancelling();
            ReportProgress("For example this action can be canceled from now on");
            Thread.Sleep(3000);
            if (CancelRequested) return Nothing();
            ReportProgress("Now back to where we were");
            Thread.Sleep(3000);
            if (CancelRequested) return Nothing();
            ReportProgress("Here is About dialog");
            Thread.Sleep(2000);
            if (CancelRequested) return Nothing();
            
            // Specifying view name is not required if view name is the same as controller action name
            return Dialog(new AboutDialogViewModel());
        }

        public ActionResult CloseDialogAndRedirect()
        {
            return 
                Redirect<HomeController>(c => c.AfterDialogPage())
                .NoNeedToSaveRequestInJournal();

            // we don't want to save navigation request that invoked this action in journal
            // to avoid clicking back and being redirected to this after dialog page again and again
        }

        public ActionResult AfterDialogPage()
        {
            return Page();
        }

        public ActionResult AppSettingsPage()
        {
            return Page(new AppSettingsViewModel());
        }
    }
}
